package framework.transport.client;

import framework.transport.codec.XResponse;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
public class RequestPool {

    private static final Map<String, CompletableFuture<XResponse<Object>>> pool = new ConcurrentHashMap<>();

    public static void put(String requestId, CompletableFuture<XResponse<Object>> future) {
        pool.put(requestId, future);
    }

    public static void complete(XResponse<Object> xResponse) {
        if ("".equals(xResponse.getRequestId()) || xResponse.getRequestId() == null) {
            log.error("requestId is null");
            return;
        }
        CompletableFuture<XResponse<Object>> future = pool.remove(xResponse.getRequestId());
        if (future == null){
            throw new IllegalStateException("request poo not have future requestId " + xResponse.getRequestId());
        }
        future.complete(xResponse);
    }

}
